#!/bin/sh
# Build an OpenStack team's package and write it in /home/ftp

set -e

if ! [ -r /etc/pkgos/pkgos.conf ] ; then
	echo "Could not read /etc/pkgos/pkgos.conf"
	exit 1
else
	. /etc/pkgos/pkgos.conf
fi

command_exists () {
	type "$1" >/dev/null 2>&1
}

# Alias git-buildpackage
if ! command_exists git-buildpackage ; then
	alias git-buildpackage="gbp buildpackage"
fi

# Some quick calculation
BPO_DISTRO_NUM=~bpo${TARGET_DISTRO_NUM}+1

cleanup_old_build () {
	echo "===> Cleaning-up old builds"
	rm -rf ../*.orig.tar.xz ../*.orig.tar.gz ../build-area
}  

# Finds the current version of the package
get_deb_version() {
	PKG_NAME=`dpkg-parsechangelog -SSource`
	DEB_VERS=`dpkg-parsechangelog -SVersion`
	NO_EPOC=`echo ${DEB_VERS} | cut -d":" -f2`
	UPSTREAM_VERS=`echo ${NO_EPOC} | cut -d"-" -f1`
	if [ "${DEB_VERS}" = "${UPSTREAM_VERS}" ] ; then IS_NATIVE="yes" ; else IS_NATIVE="no" ; fi
	ORIG=${PKG_NAME}_${UPSTREAM_VERS}.orig.tar.xz
	CHANGE=${PKG_NAME}_${NO_EPOC}_${ARCH}.changes
	PKG_NAME_FIRST_CHAR=`echo ${PKG_NAME} | awk '{print substr($0,1,1)}'`
}

create_orig_tar () {
	if grep pristine-tar debian/gbp.conf | grep -q -i true ; then
		echo "===> Calling gbp export-orig to generate the tarball"
		gbp export-orig
		echo "===> Copying tarball in ../build-area"
		mkdir -p ../build-area
		cp ../*.orig.tar.* ../build-area
	else
		if [ "${IS_NATIVE}" = "no" ] ; then
			COMPRESSION_TYPE=xz
			if [ -r debian/gbp.conf ] ; then
				COMPRESSION_IN_FILE=`cat debian/gbp.conf | grep compression | cut -d'=' -f2 | awk '{print $1}'`
				if [ "${COMPRESSION_IN_FILE}" = "gz" ] ; then
					COMPRESSION_TYPE=gz
				elif [ "${COMPRESSION_IN_FILE}" = "bzip2" ] ; then
					COMPRESSION_TYPE=bzip2
				fi
			fi
			if [ "${COMPRESSION_TYPE}" = "gz" ] ; then
				./debian/rules gen-orig-gz
			elif [ "${COMPRESSION_TYPE}" = "bzip2" ] ; then
				./debian/rules gen-orig-bz2
			else
				./debian/rules gen-orig-xz
			fi
		fi
	fi
}

bop_it () {
	echo "===> Building using git-buildpackage"
	LAST_GIT_COMMIT=`git log | head -n 1 | awk '{print $2}'`
	dch --newversion ${DEB_VERS}${BPO_DISTRO_NUM} -b --allow-lower-version -m  "Rebuilt by bop."
	git commit debian/changelog -m "Rebuilt by bop."
	pkgos-check-changelog || true
	if [ "${PKGOS_RUN_UNIT_TESTS_AT_BUILD}" = "no" ] ; then
		if ! DEB_BUILD_OPTIONS=nocheck git-buildpackage ; then
			git reset --hard ${LAST_GIT_COMMIT}
			echo "There was an error when bop called git-buildpackage: exiting."
			exit 1
		else
			git reset --hard ${LAST_GIT_COMMIT}
		fi
	else
		if ! git-buildpackage ; then
			git reset --hard ${LAST_GIT_COMMIT}
			echo "There was an error when bop called git-buildpackage: exiting."
			exit 1
		else
			git reset --hard ${LAST_GIT_COMMIT}
		fi
	fi
}

test_the_package () {
	echo "===> Lintian"
	lintian -I -E --pedantic --profile debian/openstack ../build-area/*.changes

	if [ x"$RUN_AUTOPKGTEST" = x"yes" ] ; then
		echo "===> Autopkgtest"
		set +e
		adt-run ../build-area/*.changes --- schroot "$AUTOPKGTEST_SCHROOT"
		RET=$?
		if [ $RET != 0 ] && [ $RET != 8 ] ; then
			echo "adt-run failed: $RET"
			exit $RET
		fi
		set -e
	fi
}

copy_to_ftparchive () {
	echo "===> Copying to the FTP repo"
	rm ../build-area/${PKG_NAME}_${NO_EPOC}${BPO_DISTRO_NUM}_*.build
	TARGET_FOLDER=${REPO_ROOT}/debian/pool/${TARGET_DISTRO}-${TARGET_OPENSTACK_REL}-backports/main/${PKG_NAME_FIRST_CHAR}/${PKG_NAME}
	rm -rf ${TARGET_FOLDER}
	mkdir -p ${TARGET_FOLDER}
	cp ../build-area/* ${TARGET_FOLDER}
}

cleanup_old_build
get_deb_version
create_orig_tar
bop_it
#test_the_package
copy_to_ftparchive
# Scan the repo to add the new package
pkgos-scan-repo
